using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class InteractSign : MonoBehaviour
{
    [SerializeField] private GameObject interactSign;
    private PlayerInput playerInput;
    [SerializeField] private Transform playerTran;

    private bool canPress;
    private IInteractable targetItem;

    private void Awake()
    {
        playerInput = new PlayerInput();

        playerInput.Gameplay.Interact.started += InteractAction;
        playerInput.Gameplay.EndAction.started += EndInteractAction;
    }
    

    private void OnEnable()
    {
        playerInput.Enable();
    }

    private void OnDisable()
    {
        playerInput.Disable();
    }

    private void Update()
    {
        interactSign.SetActive(canPress);
        interactSign.transform.localScale = playerTran.localScale;
    }
    
    private void InteractAction(InputAction.CallbackContext obj)
    {
        if(canPress)
            targetItem.InteractAction();
    }
    
    private void EndInteractAction(InputAction.CallbackContext obj)
    {
        if(canPress)
            targetItem.EndINteractACtion();
    }

    private void OnTriggerStay2D(Collider2D other)
    {
        if (other.CompareTag("Interactable"))
        {
            canPress = true;
            targetItem = other.GetComponent<IInteractable>();
        }
    }

    private void OnTriggerExit2D(Collider2D other)
    {
        canPress = false;
    }
}
